package com.itheima.health.controller;

import com.itheima.health.QiNiuUtils.StringUtil;
import com.itheima.health.QiNiuUtils.UploadImageService;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    UploadImageService uploadImageService;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult page = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, page);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws IOException {    //注意参数
        // 获取文件的名称
        String fileName = file.getOriginalFilename();

        // 使用工具类根据上传文件生成唯一图片名称
        String imgName = StringUtil.getRandomImgName(fileName);

        if (!file.isEmpty()) {

            FileInputStream inputStream = (FileInputStream) file.getInputStream();

            String path = uploadImageService.uploadQNImg(inputStream, imgName);
            System.out.print("七牛云返回的图片链接:" + path);
            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, path);
        }
        return new Result(true, MessageConst.PIC_UPLOAD_FAIL);
    }

    @PostMapping("/add")
    public Result add(Integer[] checkgroupIds,
                      @RequestBody Setmeal setmeal) {
        setmealService.add(setmeal, checkgroupIds);
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    @GetMapping("/selectCheckgroupIdsBySetmealId")
    public Result selectCheckgroupIdsBySetmealId(@RequestParam("id") Integer id) {
        List<Integer> ids = setmealService.selectCheckgroupIdsBySetmealId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,ids);
    }

    @PostMapping("/edit")
    public Result edit(Integer[] checkgroupIds,
                       @RequestBody Setmeal setmeal){
        setmealService.edit(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    @PostMapping("/deleteSetmealItemById")
    public Result deleteSetmealItemById(@RequestParam("id")Integer id){
        setmealService.deleteSetmealItemById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
}
